// ValidationRules.cs
//
// Copyright (c) 2007 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System;
using System.Globalization;
using System.Windows.Controls;

using Supremacy.Types;

namespace SupremacyEditor
{
    public class RangeValidationRule : ValidationRule
    {
        private double _min;
        private double _max;

        public RangeValidationRule()
        {
            this.Min = Double.MinValue;
            this.Max = Double.MaxValue;
        }

        public double Min
        {
            get { return _min; }
            set { _min = value; }
        }

        public double Max
        {
            get { return _max; }
            set { _max = value; }
        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            double number = 0;

            try
            {
                if (((string)value).Length > 0)
                    number = Number.ParseInt32((String)value);
            }
            catch (Exception e)
            {
                return new ValidationResult(false, "Illegal characters or " + e.Message);
            }

            if ((number < Min) || (number > Max))
            {
                return new ValidationResult(false,
                  "Please enter an age in the range: " + Min + " - " + Max + ".");
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }

    public class NonEmptyValidationRule : ValidationRule
    {
        public NonEmptyValidationRule() { }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            try
            {
                if (value == null)
                    return new ValidationResult(false, "Value is null");
                if (value.ToString().Trim().Length == 0)
                    return new ValidationResult(false, "Value is empty");
            }
            catch
            {
                return new ValidationResult(false, "Unhandled exception");
            }
            return new ValidationResult(true, null);
        }
    }
}
